package org.ekingstar.qd.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.Action;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.conversion.annotations.Conversion;
import com.ekingstar.framework.action.Menu;
import com.ekingstar.framework.app.ApplicationParameter;
import com.ekingstar.framework.domain.Function;
import com.ekingstar.framework.service.IRoleService;
import com.ekingstar.framework.service.IUserService;
import com.ekingstar.framework.service.impl.UserRoleServiceImpl;
import com.ekingstar.framework.util.Page;
import com.ekingstar.framework.util.ServiceLocator;
import com.ekingstar.framework.security.UserUtil;

import org.ekingstar.qd.domain.Catalog;
import org.ekingstar.qd.domain.QdDomain;
import org.ekingstar.qd.domain.QdCompareCond;
import org.ekingstar.qd.domain.QdFormula;
import org.ekingstar.qd.domain.QdMark;
import org.ekingstar.qd.domain.QdSimplemeasu;
import org.ekingstar.qd.domain.QdWhereClause;
import org.ekingstar.qd.domain.DeployQdCompareCond;
import org.ekingstar.qd.domain.DeployQdSimplemeasu;
import org.ekingstar.qd.domain.DeployQdWhereClause;
import org.ekingstar.qd.service.CMeasureService;
import org.ekingstar.qd.service.CCatalogService;
import org.ekingstar.qd.service.CConditionService;
import org.ekingstar.qd.service.CDomainService;
import org.ekingstar.qd.service.CDeployService;
import org.ekingstar.qd.service.CRelationService;


/**
 * @descript 主题发布类
 * @author 
 * @version 1.0
 */
@Action(name = "qdDomainDeploy", namespace = "/querythemes")
@Results( {
		@Result(name = "buildTree",         value = "/pages/querythemes/deploy/buildTree.jsp"),
		@Result(name = "qdDomainList",      value = "/pages/querythemes/deploy/qdDomainList.jsp"),
		@Result(name = "deployFrame",       value = "/pages/querythemes/deploy/deployFrame.jsp"),
		@Result(name = "deployRole",       value = "/pages/querythemes/deploy/deployRole.jsp"),
		@Result(name = "deployMeasure",       value = "/pages/querythemes/deploy/deployMeasure.jsp"),
		@Result(name = "deployCompareCond",       value = "/pages/querythemes/deploy/deployCompareCond.jsp"),
		@Result(name = "deployWhereClause",       value = "/pages/querythemes/deploy/deployWhereClause.jsp"),
		@Result(name = "faile",             value = "/pages/querythemes/deploy/error.jsp")})
public class DomainDeployAction extends ActionSupport{
	private static final long serialVersionUID = -1363707808689236678L;
	private CDeployService deployService;
	private CDomainService domainService;
	private CMeasureService measureService;
	private CConditionService conditionService;
	private CCatalogService catalogService;
	private CRelationService relationService;
	
	private Page page;
	private int pageNo = 1;
	private int pageSize = ApplicationParameter.INSTANCE.getPageSize();
	
	
	private String strCatalogId;
	private QdDomain searchQdDomain;
	private QdDomain qdDomain;
	private String strDomainId;
	private String strRoleId;
	private List roleList;
	private List deployRoleList;
	private List<Catalog> catalogList; 
	private List<QdSimplemeasu> simplemeasuList;
	private List<QdCompareCond> compareCondList;
	private List<QdWhereClause> whereClauseList;
	private List<QdSimplemeasu> deploySimplemeasuList;
	private List<QdCompareCond> deployCompareCondList;
	private List<QdWhereClause> deployWhereClauseList;
	
	
	public CDeployService getDeployService() {
		return deployService;
	}
	
	public void setDeployService(CDeployService deployService) {
		this.deployService = deployService;
	}
	
	public CDomainService getDomainService() {
		return domainService;
	}
	
	public void setDomainService(CDomainService domainService) {
		this.domainService = domainService;
	}
	
	public CMeasureService getMeasureService() {
		return measureService;
	}
	
	public void setMeasureService(CMeasureService measureService) {
		this.measureService = measureService;
	}
	
	public CConditionService getConditionService() {
		return conditionService;
	}
	
	public void setConditionService(CConditionService conditionService) {
		this.conditionService = conditionService;
	}
	
	public CCatalogService getCatalogService() {
		return catalogService;
	}
	
	public void setCatalogService(CCatalogService catalogService) {
		this.catalogService = catalogService;
	}
	
	public CRelationService getRelationService() {
		return relationService;
	}

	public void setRelationService(CRelationService relationService) {
		this.relationService = relationService;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getStrCatalogId() {
		return strCatalogId;
	}

	public void setStrCatalogId(String strCatalogId) {
		this.strCatalogId = strCatalogId;
	}

	public QdDomain getSearchQdDomain() {
		return searchQdDomain;
	}

	public void setSearchQdDomain(QdDomain searchQdDomain) {
		this.searchQdDomain = searchQdDomain;
	}

	public QdDomain getQdDomain() {
		return qdDomain;
	}

	public void setQdDomain(QdDomain qdDomain) {
		this.qdDomain = qdDomain;
	}

	public String getStrDomainId() {
		return strDomainId;
	}

	public void setStrDomainId(String strDomainId) {
		this.strDomainId = strDomainId;
	}

	public String getStrRoleId() {
		return strRoleId;
	}

	public void setStrRoleId(String strRoleId) {
		this.strRoleId = strRoleId;
	}

	public List getRoleList() {
		return roleList;
	}

	public void setRoleList(List roleList) {
		this.roleList = roleList;
	}
	
	public List getDeployRoleList() {
		return deployRoleList;
	}

	public void setDeployRoleList(List deployRoleList) {
		this.deployRoleList = deployRoleList;
	}

	public List<Catalog> getCatalogList() {
		return catalogList;
	}

	public void setCatalogList(List<Catalog> catalogList) {
		this.catalogList = catalogList;
	}

	public List<QdSimplemeasu> getSimplemeasuList() {
		return simplemeasuList;
	}

	public void setSimplemeasuList(List<QdSimplemeasu> simplemeasuList) {
		this.simplemeasuList = simplemeasuList;
	}

	public List<QdCompareCond> getCompareCondList() {
		return compareCondList;
	}

	public void setCompareCondList(List<QdCompareCond> compareCondList) {
		this.compareCondList = compareCondList;
	}

	public List<QdWhereClause> getWhereClauseList() {
		return whereClauseList;
	}

	public void setWhereClauseList(List<QdWhereClause> whereClauseList) {
		this.whereClauseList = whereClauseList;
	}

	public List<QdSimplemeasu> getDeploySimplemeasuList() {
		return deploySimplemeasuList;
	}

	public void setDeploySimplemeasuList(
			List<QdSimplemeasu> deploySimplemeasuList) {
		this.deploySimplemeasuList = deploySimplemeasuList;
	}

	public List<QdCompareCond> getDeployCompareCondList() {
		return deployCompareCondList;
	}

	public void setDeployCompareCondList(
			List<QdCompareCond> deployCompareCondList) {
		this.deployCompareCondList = deployCompareCondList;
	}

	public List<QdWhereClause> getDeployWhereClauseList() {
		return deployWhereClauseList;
	}

	public void setDeployWhereClauseList(
			List<QdWhereClause> deployWhereClauseList) {
		this.deployWhereClauseList = deployWhereClauseList;
	}


	//@descripte 主题查询树
	public String load4BuildTree(){
		try{
			HttpServletResponse response =  ServletActionContext.getResponse();	
			catalogList = catalogService.getCategoryList();
			List menus = new ArrayList<Menu>();
			Map m = new HashMap();
		    if (catalogList != null && !catalogList.isEmpty() && catalogList.size()>0)   {
		    	for ( int  i = 0; i < catalogList.size();i++){
		    		Catalog catalog  =(Catalog)catalogList.get(i);
		    		if(catalog!=null && catalog.getParentCatalog()!=null && catalog.getParentCatalog().getCatalogId()!=null && !"".equals(catalog.getParentCatalog().getCatalogId().toString().trim())){
		    			if(m.isEmpty()){
		    				m.put(catalog.getParentCatalog().getCatalogId().toString().trim(), "folder");
		    			}else if(m.get(catalog.getParentCatalog().getCatalogId().toString().trim())==null || "".equals(m.get(catalog.getParentCatalog().getCatalogId().toString().trim()))){
		    				m.put(catalog.getParentCatalog().getCatalogId().toString().trim(), "folder");
		    			}
		    		}		    	
		    	}
		    }
		    
		    catalogList = catalogService.getCategoryList("");
		    if (catalogList != null && !catalogList.isEmpty() && catalogList.size()>0)   {	
		    	for ( int  i = 0; i < catalogList.size();i++){
		    		Catalog catalog  =(Catalog)catalogList.get(i);		    		
		    		Menu menu = new Menu();
		    		menu.setText(catalog.getCatalogName());
		    		menu.setId(catalog.getCatalogId());	
		    		
		    		if (m.get(catalog.getCatalogId())!= null && "folder".equals(m.get(catalog.getCatalogId()).toString().trim())){
						menu.setLeaf(false);
						menu.setCls("folder");
					}else{						
						menu.setLeaf(true);
						menu.setCls("Benz");
					}
		    		//menu.setChecked(true);
		    		menus.add(menu);
		    		generateMenu(menu,catalog,m);
		    	}
		    }
	        JSONArray jsonObject = JSONArray.fromObject(menus);
	        response.setContentType("text/paint; charset=UTF-8");
			response.getWriter().print(jsonObject.toString());
		}catch(Throwable e){
			e.printStackTrace();
		}
		return null;
	}
	
	
	private void generateMenu(Menu menu,Catalog catalog,Map m){
		if (menu == null || catalog == null) return;
		menu.setText(catalog.getCatalogName());
		menu.setId(catalog.getCatalogId());		
		List childen =catalogService.getCategoryList(catalog.getCatalogId());
		if (m.get(catalog.getCatalogId())!= null && "folder".equals(m.get(catalog.getCatalogId()).toString().trim())){
			menu.setLeaf(false);
			menu.setCls("folder");
		}else{						
			menu.setLeaf(true);
			menu.setCls("Benz");
		}
		if (childen != null  && childen.size() > 0){
			List menChilden = new ArrayList();
			menu.setChildren(menChilden);
			for (int i = 0;i < childen.size();i++){
				Catalog cChilden = (Catalog)childen.get(i);
				Menu cm = new Menu();
				generateMenu(cm,cChilden,m);
				menChilden.add(cm);
			}
		}
	}
	
	//@descripte 主题查询初始化
	public String load4InitFindDomain(){
		if(this.checkUser()){
			return "qdDomainList";
		}else{
			return "faile";
		}
	}
	
	//@descripte 主题查询
	public String load4FindDomain(){
		if(this.checkUser()){
			this.page = domainService.getQdDomainList(searchQdDomain,pageNo, pageSize);		
			return "qdDomainList";
		}else{
			return "faile";
		}
	}
	
	//@descripte 加载发布主题页面主页面
	public String load4Deploy(){
		if(this.checkUser() && strDomainId!=null && !"".equals(strDomainId.toString().trim())){
			return "deployFrame";			
		}else{
			return "faile";
		}		
	}
	
	//@descripte 加载发布显示字段列页面
	public String load4DeployMeasure(){
		if(this.checkUser() && strDomainId!=null && !"".equals(strDomainId.toString().trim())){			
			qdDomain = domainService.getDomain(strDomainId);
			this.roleList = deployService.getRoleListByFunctionId(qdDomain.getFunctionId());
			if(strRoleId!=null && !"".equals(strRoleId.toString().trim())){
				deploySimplemeasuList = deployService.getDeployMeasureList(strDomainId, strRoleId);
				simplemeasuList = deployService.getMeasureList(strDomainId, strRoleId);
			}else{
				//simplemeasuList =measureService.getMeasureList(strDomainId);
				simplemeasuList = deployService.getDeployMeasureList(null, null);
				deploySimplemeasuList = deployService.getDeployMeasureList(null, null);
			}
			return "deployMeasure";			
		}else{
			return "faile";
		}
	}
	
	//@descripte 保存发布显示字段列
	public String saveOrUpdateDeployMeasure(){
		int retInt = -1;
		if(strDomainId!=null && !"".equals(strDomainId.toString().trim()) && strRoleId!=null && !"".equals(strRoleId.toString().trim())){
			String measureId = ServletActionContext.getRequest().getParameter("measureIds");
			if(measureId!=null && !"".equals(measureId.toString().trim())){
				String[] strMeasureId = measureId.split(";");
				retInt = deployService.saveOrUpdateDeployMeasure(strDomainId, strRoleId, strMeasureId);				
			}else{
				retInt = deployService.saveOrUpdateDeployMeasure(strDomainId, strRoleId, null);
			}
		}
		
		if(retInt >0){
			return this.load4DeployMeasure();
		}else{
			return "faile";
		}
	}
	
	
	//@descripte 加载发布查询字段页面
	public String load4DeployCompareCond(){
		if(this.checkUser() && strDomainId!=null && !"".equals(strDomainId.toString().trim())){			
			qdDomain = domainService.getDomain(strDomainId);
			roleList = deployService.getRoleListByFunctionId(qdDomain.getFunctionId());
			
			if(strRoleId!=null && !"".equals(strRoleId.toString().trim())){
				deployCompareCondList = deployService.getDeployCompareCondList(strDomainId, strRoleId);
				compareCondList =deployService.getCompareCondList(strDomainId,strRoleId);
			}else{
				//compareCondList =conditionService.getCompareCondList(strDomainId);
				deployCompareCondList = deployService.getDeployCompareCondList(null, null);
				compareCondList =deployService.getDeployCompareCondList(null, null);
			}
			return "deployCompareCond";			
		}else{
			return "faile";
		}
	}
	
	//@descripte 保存发布查询字段
	public String saveOrUpdateDeployCompareCond(){
		int retInt = -1;
		if(strDomainId!=null && !"".equals(strDomainId.toString().trim()) && strRoleId!=null && !"".equals(strRoleId.toString().trim())){
			String compareCondId = ServletActionContext.getRequest().getParameter("compareCondId");
			if(compareCondId!=null && !"".equals(compareCondId.toString().trim())){
				String[] strCompareCondId = compareCondId.split(";");
				retInt = deployService.saveOrUpdateDeployCompareCond(strDomainId, strRoleId, strCompareCondId);				
			}else{
				retInt = deployService.saveOrUpdateDeployCompareCond(strDomainId, strRoleId, null);
			}		
		}
		
		if(retInt >0){
			return this.load4DeployCompareCond();
		}else{
			return "faile";
		}
	}
	
	
	//@descripte 加载发布过滤条件字段页面	 
	public String load4DeployWhereClause(){
		if(this.checkUser() && strDomainId!=null && !"".equals(strDomainId.toString().trim())){			
			qdDomain = domainService.getDomain(strDomainId);
			roleList = deployService.getRoleListByFunctionId(qdDomain.getFunctionId());
			
			if(strRoleId!=null && !"".equals(strRoleId.toString().trim())){
				deployWhereClauseList = deployService.getDeployWhereClauseList(strDomainId, strRoleId);
				whereClauseList =deployService.getWhereClauseList(strDomainId,strRoleId);
			}else{
				//whereClauseList =conditionService.getWhereCauseList(strDomainId);
				deployWhereClauseList = deployService.getDeployWhereClauseList(null, null);
				whereClauseList =deployService.getDeployWhereClauseList(null, null);
			}
			return "deployWhereClause";			
		}else{
			return "faile";
		}
	}
	
	//@descripte 保存发布过滤条件字段
	public String saveOrUpdateDeployWhereClause(){
		int retInt = -1;
		if(strDomainId!=null && !"".equals(strDomainId.toString().trim()) && strRoleId!=null && !"".equals(strRoleId.toString().trim())){
			String whereClauseId = ServletActionContext.getRequest().getParameter("whereClauseId");
			if(whereClauseId!=null && !"".equals(whereClauseId.toString().trim())){
				String[] strWhereClauseId = whereClauseId.split(";");
				retInt = deployService.saveOrUpdateDeployWhereClause(strDomainId, strRoleId, strWhereClauseId);				
			}else{
				retInt = deployService.saveOrUpdateDeployWhereClause(strDomainId, strRoleId, null);
			}				
		}
		
		if(retInt >0){
			return this.load4DeployWhereClause();
		}else{
			return "faile";
		}
	}
	
	//@descripte 加载发布角色页面
	public String load4DeployRole(){
		if(this.checkUser() && strDomainId!=null && !"".equals(strDomainId.toString().trim())){			
			qdDomain = domainService.getDomain(strDomainId);
			
			deployRoleList = deployService.getDeployRoleList(qdDomain.getFunctionId());
			roleList = deployService.getRoleList(qdDomain.getFunctionId());
			
			return "deployRole";			
		}else{
			return "faile";
		}
	}
	
	//@descripte 保存发布角色
	public String saveOrUpdateDeployRole(){
		int retInt = -1;
		if(strDomainId!=null && !"".equals(strDomainId.toString().trim())){
			qdDomain = domainService.getDomain(strDomainId);
			String roleId = ServletActionContext.getRequest().getParameter("roleIds");
			if(roleId!=null && !"".equals(roleId.toString().trim())){
				String[] strRoleId = roleId.split(";");
				retInt = deployService.saveOrUpdateDeployRole(qdDomain.getFunctionId(), strRoleId);				
			}else{
				retInt = deployService.saveOrUpdateDeployRole(qdDomain.getFunctionId(), null);
			}
		}
		
		if(retInt >0){
			return this.load4DeployRole();
		}else{
			return "faile";
		}
	}
	
	private boolean checkUser(){
		boolean b = true;
		String userId = UserUtil.getCurrentUserId();
		
		if(userId==null || "".equals(userId.toString().trim())){
			b = false;
		}
		return b;
	}
	
	//@descripte 保存发布过滤条件字段
	public String testDomain(){
		int retInt = -1;
		try{
			//QdDomain qddomain = domainService.getDomain("1");
			//QdMark qdMark = qddomain.getTQdMark();			
			//System.out.println("qddomain.id == "+qddomain.getId());
			//System.out.println("qddomain.TQdMark.id == "+qdMark.getId());			
			
			//int addInt = -1;
			//addInt = domainService.addDomain("测试3-2主题001", "测试3-2主题001", "302");
			//System.out.println("addInt == "+addInt);
			
			//int editInt = -1;
			//editInt = domainService.updateDomain("1", "主题1", "主题1");
			//System.out.println("editInt == "+editInt);			
			
			//int delInt = domainService.delDomain("8a7dadfe2a0d39ae012a0d51bae70008");
			//System.out.println("delInt=="+delInt);
			
			//int copyInt = -1;
			//copyInt = domainService.copyDomain("1", "copy-主题1--to-测试2-1", "copy-主题1--to-测试2-1", "8a7dadfe29fe2ac70129fe362a65000c");
			//System.out.println("copyInt=="+copyInt);
			
			
			//String[] domainId = new String[]{"1"};
			//String removeInts = domainService.moveDomain(domainId, "1");
			//System.out.println("removeInts=="+removeInts);
			
			//List l = domainService.getDomainList("302");
			//System.out.println("l.size=="+l.size());
			
			//String[] domainId = new String[]{"8a7dadfe2a0d39ae012a0db243cf0014"};
			//int setInt = domainService.setDomainFlag(domainId, 1);
			//System.out.println("setInt=="+setInt);
			
			//String relationId = "ttt";
			//this.relationService.deleteRelation(relationId);
			//System.out.println("111");
			
			 //List tt = this.conditionService.getQdSysParamList();
			 //System.out.println("tt=="+tt.size());
			
		}catch(Exception e){
			e.printStackTrace();
			retInt = -1;
		}
		
		return "faile";
	}
}
